package com.corhyfar.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.RequestDispatcher;

import com.corhyfar.beans.Usuario;
import com.corhyfar.service.UsuarioService;
import com.corhyfar.service.impl.UsuarioServiceImpl;

/**
 * Servlet implementation class LogueoServlet
 */
@WebServlet("/LogueoServlet")
public class LogueoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LogueoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		procesar(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		procesar(request, response);
	}

	private void procesar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		UsuarioService usuarioService = new UsuarioServiceImpl();

		String codUsuario = request.getParameter("nombreUsuario");
		String claveUsuario = request.getParameter("claveUsuario");

		RequestDispatcher rd;
		Usuario obtenido = null;
		obtenido = usuarioService.validaUsuario(codUsuario, claveUsuario);
		if (obtenido != null) {
			rd = request.getRequestDispatcher("/Corhyfar_AreaUsuario.jsp");
		} else {
			rd = request.getRequestDispatcher("/Corhyfar_Login.jsp");
		}
		rd.forward(request, response);

	}

}
